package com.resourcestore.action;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.resourcestore.utils.CommonUtil;
import com.resourcestore.utils.tag.Pagination;
import com.resourcestore.action.BaseAction;
import com.resourcestore.dao.RolesDao;
import com.resourcestore.model.Roles;
import com.resourcestore.dao.OperationsDao;
import com.resourcestore.model.Operations;

public class RolesAction extends BaseAction {

	private Roles roles;
	private List<Roles> listRoles;
	private List<Operations> listAllCheck;
	private List<String> listChecked = new ArrayList<String>();
	private Operations operations;
	private RolesDao rolesDao = new RolesDao();
	private OperationsDao operationsDao = new OperationsDao();
	private Pagination pagination = new Pagination();

	/**
	 * 查询返回List带分页
	 * 
	 * @throws ParseException
	 * 
	 */
	public String listRoles() {

		roles = new Roles();
		pagination.setTotal(rolesDao.queryRolesNum(roles));
		listRoles = rolesDao.selectRolesPage(roles, pagination);

		dealOperCollection(listRoles);

		return "default";
	}

	/**
	 *处理operCollection字符串
	 * 
	 * @throws ParseException
	 * 
	 */
	public void dealOperCollection(List<Roles> list) {
		String str = null;
		String[] s = null;
		Operations operationsTemp = new Operations();
		String sTemp = null;

		for (int i = 0; i < list.size(); i++) {
			try {
				list.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(list.get(i)
								.getGmtCreated()));
				list.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(list.get(i)
								.getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			str = list.get(i).getOperCollection();
			if (str != null) {
				s = CommonUtil.split(str, ",");
				sTemp = "";
				for (int j = 0; j < s.length; j++) {
					operationsTemp.setOperId(Integer.valueOf(s[j]));
					if (CommonUtil.isNotNullOrNotEqualsEmpty(operationsDao
							.selectOperationsById(operationsTemp))) {
						operationsTemp = operationsDao
								.selectOperationsById(operationsTemp);
					} else {
						operationsTemp.setOperName("未知模块");
					}
					sTemp = sTemp + "<div class='noMoreLine'>["
							+ operationsTemp.getOperName() + "]</div>";
				}
				list.get(i).setStringShowOperationsName(sTemp);
			}

		}
	}

	/**
	 * 根据条件搜索，查询返回List带分页
	 * 
	 * @throws ParseException
	 * 
	 */
	public String searchListRoles() {

		// 对内容进行urldecoder解码
		try {
			roles.setRoleName(new String(roles.getRoleName().getBytes(
					"iso-8859-1"), "UTF-8"));
			roles.setRemark(new String(
					roles.getRemark().getBytes("iso-8859-1"), "UTF-8"));
			roles.setAuthor(new String(
					roles.getAuthor().getBytes("iso-8859-1"), "UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		pagination.setTotal(rolesDao.queryRolesNum(roles));
		listRoles = rolesDao.selectRolesPage(roles, pagination);

		dealOperCollection(listRoles);

		return newpath("/Roles/listRoles.jsp");
	}

	/**
	 * 预处理插入信息
	 */

	public String preInsertRoles() {
		// listAllCheck = operationsDao.selectOperations();

		return newpath("/Roles/insertRoles.jsp");

	}

	/**
	 * 插入一条数据
	 */
	public String insertRoles() {

		try {
			roles.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			roles.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		rolesDao.insertRoles(roles);
		return redirect(this.getUrl());
	}

	/**
	 * 更新之前加载原信息
	 * 
	 */
	public String preUpdateRoles() {

		roles = rolesDao.selectRolesById(roles);

		String str = "";
		str = roles.getOperCollection();
		if (str != null) {
			String[] s = CommonUtil.split(str, ",");
			for (int i = 0; i < s.length; i++) {
				listChecked.add(i, s[i]);
			}
		}

		// listAllCheck = operationsDao.selectOperations();

		return newpath("/Roles/updateRoles.jsp");
	}

	/**
	 * 更新信息
	 * 
	 */
	public String updateRoles() {

		try {
			// roles.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			roles.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		rolesDao.updateRoles(roles);
		return redirect(this.getUrl());
	}

	/**
	 * 删除信息
	 */
	public String deleteRoles() {
		rolesDao.deleteRoles(roles);
		return redirect(this.getUrl());
	}

	/**
	 * 批量删除信息
	 */
	public String deleteMoreRoles() {
		
		rolesDao.deleteMoreRoles(roles);
		return redirect(this.getUrl());
	}

	public Roles getRoles() {
		return roles;
	}

	public void setRoles(Roles roles) {
		this.roles = roles;
	}

	public List<Roles> getListRoles() {
		return listRoles;
	}

	public void setListRoles(List<Roles> listRoles) {
		this.listRoles = listRoles;
	}

	public List<Operations> getListAllCheck() {
		return listAllCheck;
	}

	public void setListAllCheck(List<Operations> listAllCheck) {
		this.listAllCheck = listAllCheck;
	}

	public Operations getOperations() {
		return operations;
	}

	public void setOperations(Operations operations) {
		this.operations = operations;
	}

	public List<String> getListChecked() {
		return listChecked;
	}

	public void setListChecked(List<String> listChecked) {
		this.listChecked = listChecked;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

}
